<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas id="canvas" height="600" width="700"></canvas>
  <script>
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.beginPath()
    ctx.font = '100px Verdana' // 改变字的大小，文本的长度也会发生变化
    ctx.textAlign = 'center'
    ctx.textBaseline = 'middle'
    ctx.strokeText('Hello Canvas!', 350, 300, 400)
    const width = ctx.measureText('Hello Canvas!').width
    console.log('文本的宽度为：', width);
  </script>
</body>

</html>